﻿using System;
using System.Collections.Generic;
using Microsoft.AspNetCore.Mvc;
using QQZiFramework.Mvc;
using QQZiFramework;
using QQZiCore.Mvc;
using QQZiFramework.Membership;
// For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860

namespace QQZiCore.SH3H.Api.Controllers
{
    /// <summary>
    /// 常规接口
    /// </summary>
    [LoginCheckFilter(IsCheck=false)]
    public class HomeController : QQZiCore.Mvc.Controllers.BaseController

    {

        [HttpGet]
        [HttpPost]
        public string TestWait(int s)
        {
            System.Threading.Thread.Sleep(s * 1000);
            return "ok";
        }

        /// <summary>
        /// 清缓存，重新加载配置
        /// </summary>
        [HttpGet]
        [LoginCheckFilter(IsCheck = false, CheckPermission = null, WeixinAuthorize = WeixinAuthorizeState.Disable)]
        public string ReloadConfig()
        {
            QQZiFramework.Modals.OEMInfo CurrentOEM = CreateDBContext().OEM;
            List<QQZiFramework.Modals.OEMInfo> lst = null;
            if (CurrentOEM != null)
            {
                lst = new List<QQZiFramework.Modals.OEMInfo>();
                lst.Add(CurrentOEM);
            }
            else lst = OEMMemberShip.OEMList;
            WebHostMemberShip.ReloadConfig();
            foreach (QQZiFramework.Modals.OEMInfo oem in lst)
            {
                OEMMemberShip memberShip = OEMMemberShip.GetInstance(oem.Name);
                DBContext oemContext = memberShip.CreateDBContext();
                //oemContext.ChearCacheLike("NearInforProduct");
                //oemContext.ChearCacheLike("NearInforMerchant");
                //oemContext.ChearCacheLike("Adertising");
                //oemContext.ChearCacheLike("CrazyBuy");
                //oemContext.ChearCacheLike("NoticeNews");
                //oemContext.ChearCacheLike("merchants");
                //oemContext.ChearCacheLike("Citys");
                //oemContext.ChearCacheLike("City_BusinessCircleList");
                oemContext.Membership.ClearCache(oemContext);
            }

            return "success";
        }



        /// <summary>
        /// 图片上传，转传到图片服务器
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [QQZiFramework.Mvc.LoginCheckFilter(IsCheck = false, CheckPermission = null, WeixinAuthorize = WeixinAuthorizeState.Disable)]
        public string UploadImage()
        {
            int istemp = GetParamValue<int>("istemp", false, 0);
            using (DBContext context = CreateDBContext())
            {
                QQZiFramework.JsonMessage jm = new QQZiFramework.JsonMessage();
                string server_bg = GetAppSetting<string>("ImageServer:UploadBG", '/', false);
                string server_out = GetAppSetting<string>("1", '/', false);
                if (server_out == null) server_out = GetAppSetting<string>("ImageServer", '/', false);
                if (server_bg == null) server_bg = server_out;
                else if (server_out == null) server_out = server_bg;
                //oem是否有重载
                server_bg = context.GetSettingValue<string>("ImageServer:UploadBG", false, server_bg);
                server_out = context.GetSettingValue<string>("ImageServer:AccessOut", false, server_out);

                string actionUrl;
                if (string.IsNullOrEmpty(server_bg) || server_bg == "." || server_bg == "./" || server_bg.StartsWith(Request.Host.Value))
                {
                    jm = this.UploadImage(null, istemp == 1, 5242880);
                    if (jm.Result != null)
                    {
                        string url = (string)jm.Result;
                        if (!url.StartsWith("http")) jm.Result = (string.IsNullOrEmpty(server_out) ? DomainServer : server_out) + url;
                    }
                }
                else
                {
                    actionUrl = server_bg + context.OEM.Name + "/Home/UploadImage";
                    jm = this.UploadImage(actionUrl, istemp == 1, 5242880);//5M限制
                    if (jm.Message == null)
                    {
                        string url = jm.Result.ToString();
                        if (!url.StartsWith("http")) jm.Result = server_out + url;
                    }
                }
                return jm.ToJson();
            }
        }

    }
}
